<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">

            <!-- 放置表单 -->
            <el-form ref="userForm" :model="formData" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="formData.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password">
                <el-input v-model="formData.password" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="btnOK">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <!-- <UserInfo /> -->
            <!-- 上面是具体组件标签, 也可以用动态组件实现 -->
            <component :is="page1" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <!-- <JobInfo /> -->
            <component :is="page2" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetail, saveUserDetail } from '@/api/user'
import UserInfo from './components/user-info.vue'
import JobInfo from './components/job-info.vue'

export default {
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      page1: 'UserInfo',
      page2: 'JobInfo',
      //  对于在url上传参的数据都存在于 this.$route 属性里面
      // 其中 xxx/:xxx 的东西存放在 this.$route.params.xxx
      // 另外如果是 xxx?xxx=1 存放在 this.$route.query.xxx
      userId: this.$route.params.id,
      formData: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 5, message: '名字不能超过五个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 12, message: '密码不能超过12位', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    // 因为获取回来的数据, 密码呢是经过加密的混乱字符串, 需要清空, 让用户修改时重新填写
    this.formData = {
      ...await getUserDetail(this.userId),
      password: ''
    }
  },
  methods: {
    async btnOK() {
      // 校验表单
      await this.$refs.userForm.validate()
      // 发送请求
      await saveUserDetail(this.formData)
      // 提示用户
      this.$message.success('操作成功')
    }
  }
}
</script>

<style>

</style>
